<?php

namespace App\Http\Controllers\Admin;

use App\Models\ResourceBusinessName;
use App\Models\ResourceTotal;
use App\Validate\ResourceTotalValidate;
use Illuminate\Support\Facades\DB;

/**
 * 数字资源总数据管理
 */
class ResourceTotalController extends CommonController
{
    public $model = null;
    public $validate = null;

    public function __construct()
    {
        parent::__construct();

        $this->validate = new ResourceTotalValidate();
        $this->model = new ResourceTotal();
    }

    /**
     * 检索 mysql 数据 （全数据检索）
     * @param keywords 检索条件
     * @param big_type_id 系统大类  （系统内定，不可修改）  系统大类 1 电子书  2 有声书（音频） 3 视频
    // * @param type_id 系统小类
     * @param page int 当前页
     * @param limit int 分页大小
     */
    public function lists()
    {
        $keywords = $this->request->input('keywords', '');
        $big_type_id = $this->request->input('big_type_id', '');
        $limit = $this->request->input('limit', 10);
        $page = $this->request->input('page', 1);
        $res = $this->model->lists($keywords, $big_type_id, null, null, null, null, $limit);

        if ($res['data']) {
            foreach ($res['data'] as $key => $val) {
                //增加序号
                $res['data'][$key][$this->list_index_key] = $this->addSerialNumberOne($key, $page, $limit);
                $res['data'][$key]['big_type_name'] = !empty($val['con_big_type']) ? $val['con_big_type']['type_name'] : '';
                unset($res['data'][$key]['con_big_type']);
            }
            $res = $this->disPageData($res);
            return $this->returnApi(200, '获取成功', true, $res);
        }

        return $this->returnApi(203, '暂无数据');
    }


    /**
     * 获取资源详情
     * @param guid  资源guid
     */
    public function detail()
    {
        //增加验证场景进行验证
        if (!$this->validate->scene('detail')->check($this->request->all())) {
            return $this->returnApi(201,  $this->validate->getError());
        }

        $res = $this->model->detail(null, $this->request->guid);
        if (empty($res)) {
            return $this->returnApi(203, "暂无数据");
        }

        $data = $res->toArray();
        $data['type_name'] = !empty($data['con_type']) ? $data['con_type']['type_name'] : '';
        $data['big_type_name'] = !empty($data['con_big_type']) ? $data['con_big_type']['type_name'] : '';
        unset($data['con_type']);
        unset($data['con_big_type']);

        return $this->returnApi(200, "获取成功", true, $data);
    }


    /**
     * 删除
     * @param guid  资源guid
     */
    public function del()
    {
        //增加验证场景进行验证
        if (!$this->validate->scene('del')->check($this->request->all())) {
            return $this->returnApi(201,  $this->validate->getError());
        }

        $guid = $this->request->guid;
        $res = $this->model->where('guid', $guid)->where('is_del', 1)->first();
        if (!$res) {
            return $this->returnApi(201, '参数传递错误');
        }

        DB::beginTransaction();
        try {
            $res->is_del = 2;
            $res->save();
            //删除各自文件的资源
            $model = $this->model->getResourceTotalModel($res->resource_business_id);
            if (empty($model)) {
                throw new \Exception('单个资源商未找到');
            }
            $result = $model->where('guid', $guid)->where('is_del', 1)->update(['is_del' => 2]);
            if (!$result) {
                throw new \Exception('删除失败');
            }
            DB::commit();
            return $this->returnApi(200, '删除成功', true);
        } catch (\Exception $e) {
            DB::rollback();
            return $this->returnApi(202, $e->getMessage());
        }
    }
}
